package echecs.engine;

import echecs.tools.FENUtils;
import echecs.tools.Piece;
import echecs.tools.Square;
import echecs.vue.BoardVue;
import javax.swing.JApplet;

public abstract class Engine extends BoardVue implements Runnable {

    public static int color = 1;	//color of the player that can move
    Thread th = null;	//AI thread
    public static int movecounter = 0;
    public static int deep = 0;	//actual deep
    public static int target = 4;	//target deep
    public static float value = 0;	//minimax
    public static int move;//move of the AI

    public Engine(JApplet ref) {
        super(ref);
    }

    public void run() {

        movecounter = 0;
        genmove();//TODO

        if (movecounter == 0) //no moves -> end of game
        {
            if (ischeck()) {
                parent.getAppletContext().showStatus("white wins!");
            } else {
                parent.getAppletContext().showStatus("game is a draw!");
            }
            return;
        }
        printMoveList();
        execute(move / 100, move % 100);

    }

    public void execute(int start, int end) {
        board[end] = board[start];
        board[start] = 0;
        //Rochade ?
        if (board[end] % 10 == 6) {
            if (end == start + 2) {
                //little
                board[start + 1] = board[start + 3] % 100;
                board[start + 3] = 0;
                graphboard[start + 1] = board[start + 1];
                graphboard[start + 3] = 0;
                paintSquare(start + 3);
                paintSquare(start + 1);
            }
            if (end == start - 2) {
                //big
                board[start - 1] = board[start - 4] % 100;
                board[start - 4] = 0;
                graphboard[start - 1] = board[start - 1];
                graphboard[start - 4] = 0;
                paintSquare(start - 4);
                paintSquare(start - 1);
            }
        }
        //change pawn?
        if ((board[end] % 10 == 1) && ((end < 29) || (end > 90))) {
            board[end] += 4;
        }
        graphboard[start] = board[start];
        graphboard[end] = board[end];
        paintSquare(end);
        paintSquare(start);
        //change player
        if (color == 1) {
            color = 2;
            th = new Thread(this);
            th.setPriority(10);
            th.start();
        }
//        else {
////            color = 1;
////            //calculate valid moves
////            movecounter = 0;
////            deep = 0;
////            target = 1;
//
////            genmove();//TODO
//            printMoveList();
////            if (movecounter == 0) {
////                if (ischeck()) {
////                    parent.getAppletContext().showStatus("Black wins!");
////                } else {
////                    parent.getAppletContext().showStatus("Game is a draw!");
////                }
////            }
//        }
    }

    public void enterFenPosition(String fenPosition) {
        /////
        if (parent != null) {
            parent.getAppletContext().showStatus("");
        }
        //kill AI thread
        if (th != null) {
            th.stop();
        }
        th = null;
        /////
        Piece[][] chessboard = FENUtils.toBoard(fenPosition).getPieces();
        int[][] org2D = new int[64][64];
        int[] org = new int[120];
        Square sq;
        int couleur = 0;
        Piece piece;
        int forme = 0;
        for (int l = 0; l < 120; l++) {
            org[l] = 99;
        }
        for (int l = 0; l < 8; l++) {
            for (int c = 0; c < 8; c++) {
                org2D[c][l] = 99;
            }
        }
        for (int l = 0; l < 8; l++) {
            for (int c = 0; c < 8; c++) {
                sq = Square.valueOf(c, l);
                piece = chessboard[c][l];

                if (chessboard[c][l] == null) {
                    org2D[c][l] = 0;

                } else if (chessboard[c][l].isWhite()) {
                    couleur = 1;
                    forme = piece.getType().getCode();
                    org2D[c][l] = 10 * couleur + forme;
                    if ((10 * couleur + forme == 14) || (10 * couleur + forme == 24) || (10 * couleur + forme == 16) || (10 * couleur + forme == 26)) {
                        org2D[c][l] += 100;
                    }
                } else {
                    couleur = 2;
                    forme = piece.getType().getCode();
                    org2D[c][l] = 10 * couleur + forme;
                    if ((10 * couleur + forme == 14) || (10 * couleur + forme == 24) || (10 * couleur + forme == 16) || (10 * couleur + forme == 26)) {
                        org2D[c][l] += 100;
                    }
                }
            }
        }
        for (int c = 0; c < 8; c++) {
            for (int l = 0; l < 8; l++) {
                org[21 + l * 10 + c] = org2D[c][7 - l];
            }
        }

        for (int i = 0; i < 120; i++) {
            board[i] = org[i];
            graphboard[i] = org[i];
        }
        repaint();
        movecounter = 0;
        color = 1;
        deep = 0;
        target = 1;

        genmove();//TODO
        printMoveList();
        code = 0;
    }
}
